import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.JPanel;

//all the outcomes you can have in yahtzee all int 
public class Scoreboard extends JPanel {
	static final int MAX_DICE = 5;
	static final int KIND3 = 6;
	static final int KIND4 = 7;
	static final int F_HOUSE = 8;
	static final int S_STRAIGHT = 9;
	static final int L_STRAIGHT = 10;
	final int MAX_TEXT_WIDTH = 450;
	final int MAX_TEXT_H = 30;
	final int MAX_ITEMS = 13;
	final int MAX_DICE_SLOTS = 6;
	final int MAX_3KIND = 3;
	final int MAX_4KIND = 4;
	final int MAX_STRAIGHT = 5;

	final int FHOUSE = 250;
	final int SSTRAIGHT = 350;
	final int LSTRAIGHT = 400;
	final int OMEGA_SCORE = 9000;

	ScoreObj[] score;
	int text_height;
	boolean firstRun = true;

	// font's for all the color in the draw
	Font f = new Font("Whiskey Bravo Victor", Font.BOLD | Font.ITALIC, 30);
	FontMetrics fm;

	// score board for the class
	public Scoreboard() {
		// TODO Auto-generated constructor stub
		score = new ScoreObj[MAX_ITEMS];
		text_height = 0;
		String t_name = null;

		for (int i = 0; i < score.length; i++) {
			score[i] = new ScoreObj();
		}

		File f = new File("names.txt");
		try {
			FileReader infile = new FileReader(f);
			BufferedReader bufRead = new BufferedReader(infile);

			for (int i = 0; i < score.length; i++) {
				try {
					t_name = bufRead.readLine();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					System.out.println("IO exception =" + e);
				}
				score[i].setName(t_name);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("File Not Found exception =" + e);
		}
	}

	// update
	void update(Dice[] dice, Coordinates a_position, Coordinates a_screen) {
		for (int j = 0; j < MAX_ITEMS; j++) {
			if (score[j].getTextRect().contains(a_position.getX(),
					a_position.getY())) {
				System.out.println("score: " + j);
				switch (j) {
				case 0: // fall through - case bracket
				case 1: // for proper case;
				case 2:
				case 3:
				case 4:
				case 5:
				case 6:
				case 7:
				case 8:
				case 9:
				case 10:
				case 11:
				case 12:
					// ! == score this item
					if (!score[j].isBeenScored())
						setScores(j, dice);
					break;
				}
			}
		}
	}
	
	// draw
	void draw(Graphics g) {
		g.setFont(f);
		g.setColor(Color.BLACK);
		if(firstRun)
		{
			for (int i = 0; i < score.length; i++) {
				switch (i) {
				case 0:
					score[i].setTextRect(new Rectangle(MAX_TEXT_WIDTH, MAX_TEXT_H, MAX_TEXT_WIDTH, MAX_TEXT_H));
					break;
				default:
					score[i].setTextRect(new Rectangle(MAX_TEXT_WIDTH, i * MAX_TEXT_H, MAX_TEXT_WIDTH, (i+1) * MAX_TEXT_H));
					break;
				}
				System.out.println("bounds[" + i + "]: " + score[i].getTextRect());
			}
			firstRun = false;
		}
		
		for (int i = 0; i < MAX_ITEMS; i++) {
			switch (i) {
			case 0:
				g.drawString(String.valueOf(score[i].getScore()).concat(" " + score[i].getName()), MAX_TEXT_WIDTH, (2 * text_height));
				System.out.println("x: " + MAX_TEXT_WIDTH + " | y: " + (2 * text_height));
				break;
			default:
				g.drawString(String.valueOf(score[i].getScore()).concat(" " + score[i].getName()), MAX_TEXT_WIDTH, (i+1) * (2 * text_height));
				System.out.println("x: " + MAX_TEXT_WIDTH + " | y: " + ((i+1) * (2 * text_height)));
				break;
			}
		}
	}

	// this sets the scores
	void setScores(int a_index, Dice[] dice) {
		int i = 0;
		int t_score = 0;
		switch (a_index) {
		case 0: // ones
			for (i = 0; i < MAX_DICE; i++) {
				if (dice[i].getDiceValue() == 1) {
					t_score++;
				}
			}
			t_score += (t_score * 100);
			score[a_index].setScore(t_score);
			score[a_index].setBeenScored(true);
			break;
		case 1: // two
			for (i = 0; i < MAX_DICE; i++) {
				if (dice[i].getDiceValue() == 2) {
					t_score++;
				}
			}
			score[a_index].setScore(t_score += (t_score * 200));
			score[a_index].setBeenScored(true);
			break;
		case 2: // three
			for (i = 0; i < MAX_DICE; i++) {
				if (dice[i].getDiceValue() == 3) {
					t_score++;
				}
			}
			t_score += (t_score * 300);
			score[a_index].setScore(t_score);
			score[a_index].setBeenScored(true);
			break;
		case 3: // four
			for (i = 0; i < MAX_DICE; i++) {
				if (dice[i].getDiceValue() == 4) {
					t_score++;
				}
			}
			t_score += (t_score * 400);
			score[a_index].setScore(t_score);
			score[a_index].setBeenScored(true);
			break;
		case 4: // fives
			for (i = 0; i < MAX_DICE; i++) {
				if (dice[i].getDiceValue() == 5) {
					t_score++;
				}
			}
			t_score += (t_score * 500);
			score[a_index].setScore(t_score);
			score[a_index].setBeenScored(true);
			break;
		case 5: // sixes
			for (i = 0; i < MAX_DICE; i++) {
				if (dice[i].getDiceValue() == 6) {
					t_score++;
				}
			}
			t_score += (t_score * 600);
			score[a_index].setScore(t_score);
			score[a_index].setBeenScored(true);
			break;
		case 6: // 3 of a kind
			if (checkSpecialScores(a_index, dice)) {
				score[a_index].setBeenScored(true);
			}
			break;
		case 7: // 4 of a kind
			if (checkSpecialScores(a_index, dice)) {
				score[a_index].setBeenScored(true);
			}
			break;
		case 8: // Full House
			if (checkSpecialScores(a_index, dice)) {
				score[a_index].setScore(FHOUSE);
				score[a_index].setBeenScored(true);
			}
			break;
		case 9: // Small Straight
			if (checkSpecialScores(a_index, dice)) {
				score[a_index].setScore(SSTRAIGHT);
				score[a_index].setBeenScored(true);
			}
			break;
		case 10: // Large Straight
			if (checkSpecialScores(a_index, dice)) {
				score[a_index].setScore(LSTRAIGHT);
				score[a_index].setBeenScored(true);
			}
			break;
		case 11: // Omega
			for (i = 0; i < MAX_DICE; i++) {
				if ((i + 1) != MAX_DICE) {
					if (dice[i].getDiceValue() == dice[i + 1].getDiceValue())
						t_score++;
				} else {
					if (dice[i].getDiceValue() == dice[0].getDiceValue())
						t_score++;
				}
			}
			if (t_score == MAX_DICE) {
				score[a_index].setScore(OMEGA_SCORE);
				score[a_index].setBeenScored(true);
			}
			break;
		case 12: // chance
			for (i = 0; i < MAX_DICE; i++) {
				t_score += dice[i].getDiceValue() * 10;
			}
			score[a_index].setScore(t_score);
			score[a_index].setBeenScored(true);
			break;
		}

	}

	// check for any different scores from the normal
	boolean checkSpecialScores(int a_case, Dice[] dice) {
		int i = 0;
		int[] t_dice = new int[MAX_DICE_SLOTS];
		boolean t_result = false;
		int t_num = 0, t_score = 0;
		boolean t_checked = false;

		// initialize t_dice
		for (int h = 0; h < MAX_DICE_SLOTS; h++) {
			t_dice[h] = 0;
		}
		// fill t_dice with the numbers
		for (i = 0; i < MAX_DICE; i++) {
			switch (dice[i].getDiceValue()) {
			case 1: // value 1
				t_dice[0]++;
				break;
			case 2: // value 2
				t_dice[1]++;
				break;
			case 3: // value 3
				t_dice[2]++;
				break;
			case 4: // value 4
				t_dice[3]++;
				break;
			case 5: // value 5
				t_dice[4]++;
				break;
			case 6: // value 6
				t_dice[5]++;
				break;
			}
		}

		// depending on case check if true
		switch (a_case) {
		case KIND3: // checks for three of a kind and gives appropriate scoring
			for (i = 0; i < MAX_DICE_SLOTS; i++) {
				t_score = 0;
				switch (t_dice[i]) {
				case 1:
					t_score += (i + 1) * 10;
					score[a_case].setScore(t_score);
					break;
				case 2:
					t_score += (i + 1) * 20;
					score[a_case].setScore(t_score);
					break;
				case 3:
					t_score += (i + 1) * 30;
					score[a_case].setScore(t_score);
					break;
				}
			}
			t_result = true;
			break;
		case KIND4: // checks for 4 of a kind and gives appropriate scoring
			for (i = 0; i < MAX_DICE_SLOTS; i++) {
				t_score = 0;
				switch (t_dice[i]) {
				case 1:
					t_score += (i + 1) * 10;
					score[a_case].setScore(t_score);
					break;
				case 4:
					t_score += (i + 1) * 40;
					score[a_case].setScore(t_score);
					break;
				}
			}
			t_result = true;
			break;
		case F_HOUSE: // checks for 3 of a kind and a pair
			boolean isPair = false;
			boolean isTriple = false;
			for (i = 0; i < MAX_DICE_SLOTS; i++) {
				switch (t_dice[i]) {
				case 2:
					isPair = true;
					break;
				case 3:
					isTriple = true;
					break;
				}
			}
			if (isPair && isTriple)
				t_result = true;
			else
				t_result = false;
			break;
		case S_STRAIGHT: // checks that 4 of the dice are sequential. (1-4 or
			// 2-5 or 3-6)
			t_num = 0;
			t_checked = false;
			for (i = 0; i < MAX_DICE_SLOTS; i++) {
				if (t_num != MAX_4KIND) {
					switch (t_dice[i]) {
					case 1:
						if (t_num == (MAX_4KIND - 1)) {
							// if the last dice in the series == 1
							// add this to the t_count
							if (t_dice[i] == 1)
								t_num++;
						} else if ((i + 1) != MAX_DICE_SLOTS) {
							if (t_dice[i] == t_dice[i + 1])
								t_num++;
							else if (t_dice[i] == t_dice[i + 1] - 1)
								t_num++;
						} else if (t_dice[i] == 1)
							t_num++;
						break;
					case 2:
						if (!t_checked) {
							if (t_num == (MAX_DICE_SLOTS - 1)) {
								// if the last dice in the series == 1
								// add this to the t_count
								if (t_dice[i] == 1)
									t_num++;
							} else if ((i + 1) != MAX_DICE_SLOTS) {
								if (t_dice[i] - 1 == t_dice[i + 1])
									t_num++;
							}
							t_checked = true;
						}
						break;
					}
				}
			}
			if (t_num == MAX_4KIND)
				t_result = true;
			else
				t_result = false;
			break;
		case L_STRAIGHT: // checks that all of the dice are sequential. (1-5 or
			// 2-6)
			t_num = 0;
			t_checked = false;
			for (i = 0; i < MAX_DICE_SLOTS; i++) {
				if (t_num != MAX_STRAIGHT) {
					switch (t_dice[i]) {
					case 1:
						if (t_num == (MAX_STRAIGHT - 1)) {
							// if the last dice in the series == 1
							// add this to the t_count
							if (t_dice[i] == 1)
								t_num++;
						} else if ((i + 1) != MAX_DICE_SLOTS) {
							if (t_dice[i] == t_dice[i + 1])
								t_num++;
						}
						break;
					case 2:
						if (!t_checked) {
							if (t_num == (MAX_DICE_SLOTS - 1)) {
								// if the last dice in the series == 1
								// add this to the t_count
								if (t_dice[i] == 1)
									t_num++;
							} else if ((i + 1) != MAX_DICE_SLOTS) {
								if ((t_dice[i] - 1) == t_dice[i + 1])
									t_num++;
							}
							t_checked = true;
						}
						break;
					}
				}
			}
			if (t_num == MAX_STRAIGHT)
				t_result = true;
			else
				t_result = false;
			break;
		}
		return t_result;
	}

	public void setText_height(int textHeight) {
		text_height = textHeight;
	}

	public int getWidth() {
		return MAX_TEXT_WIDTH;
	}

	int getHieght() {
		return text_height;
	}

	int getTextWidth(int a_index) {
		return score[a_index].getText_width();
	}

	int getItems() {
		return MAX_ITEMS;
	}

	boolean isScored(int a_index) {
		return score[a_index].isBeenScored();
	}

}